package org.admin.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.UpdateJoinWrapper;
import core.data.DataHandle;
import org.admin.entity.domain.PermsGroup;
import org.admin.entity.domain.Role;
import org.admin.entity.domain.RolePermsGroup;
import org.admin.entity.dto.Power;
import org.admin.entity.dto.PowerDto;
import org.admin.mapper.RolePermsGroupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class PowerService {
    @Autowired
    private RolePermsGroupMapper mapper;

    @Transactional
    public void update(DataHandle<PowerDto, ?> handle) {
        PowerDto params = handle.getParams();
        Date now = new Date();
        MPJLambdaWrapper<Role> roleQuery = new MPJLambdaWrapper<>(Role.class);
        if(roleQuery.eq(Role::getId, params.getRoleId()).count() == 0){
            handle.failure("roleId", "指定角色不存在");
            return;
        }

        List<RolePermsGroup> list = mapper.selectList(MPJWrappers.lambdaJoin(RolePermsGroup.class)
                .eq(RolePermsGroup::getRoleId, params.getRoleId())
                .disableLogicDel());

        Map<Integer, Power> powerMap = new HashMap<>();
        Set<Integer> powerList= new HashSet<>();
        // 递归权限数据
        for(Power item : params.getPowers()) {
            powerMap.put(item.getGroupId(), item);
            powerList.add(item.getGroupId());
        }
        // 校验group 是否存在

        if(!powerList.isEmpty() &&  MPJWrappers.lambdaJoin(PermsGroup.class)
                .in(PermsGroup::getId, powerList)
                .count() != powerList.size()){
            handle.failure("powers", "指定了不存在的接口组");
            return;
        }

        for(RolePermsGroup item: list){
            if(powerMap.containsKey(item.getGroupId())){
                mapper.recover(item.getId(), powerMap.get(item.getGroupId()).getPower());
                powerMap.remove(item.getGroupId());
            }else{
                // item.setDeletedAt(now);
                mapper.deleteById(item);
            }

        }

        for(Power item : powerMap.values()){
            mapper.insert(new RolePermsGroup() {{
                setCreatedAt(now);
                setUpdatedAt(now);
                setPower(item.getPower());
                setRoleId(params.getRoleId());
                setGroupId(item.getGroupId());
            }});
        }
        handle.success();
    }

    public void list(DataHandle<PowerDto, List<Power>> handle) {
        PowerDto params = handle.getParams();
        if(MPJWrappers.lambdaJoin(Role.class)

                .eq(Role::getId, params.getRoleId())
                .count() == 0){
            handle.failure("roleId", "指定角色不存在");
            return;
        }
        List<Power> list = MPJWrappers.lambdaJoin(RolePermsGroup.class)
                .select(RolePermsGroup::getGroupId, RolePermsGroup::getPower)
                .eq(RolePermsGroup::getRoleId, params.getRoleId())
                .list(Power.class);
        handle.success(list);
    }
}
